// pages/sublimit/record/index.js
import {Http} from '../../../utils/http'
import {Balance} from '../../../model/Balance'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    slist:[{
      id:0,
      title:'95598',
      createtime:'2020-04-07',
      money:0.02,
      pm:0,
    }],
    list:[],
    serverDatas:[],
    pageSize: 20,
    totalPage: 1,
    start: 1,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let http = await Http.login();
    let balace = await Balance.get_Withdrawal_list();
    this.push(balace);
    this.setData({
      list: this.data.slist,
      serverDatas: this.data.slist,
      loading:true
    });
  },

  push(balace) {
    this.setData({
      slist:balace.data
    })
  },

  //上拉加载 下拉刷新开始
  getList: function(start, pageSize) {
    if(this.data.slist==''){
      this.setData({
        show:true
      })
    }else{
      this.setData({
        show:false
      })
    }
    var serverDatas = this.data.serverDatas
    var totalCount = serverDatas.length
    var list = new Array()
    var length = start * pageSize >= serverDatas.length ? serverDatas.length : start * pageSize
    for(var i=(start - 1) * pageSize; i<length; i++) {
      list.push(this.data.slist[i])
    }
    var result = {
      totalCount: totalCount,
      list: list
    }
    console.log("刷新");
    return new Promise(function (resolve, reject) {
      //模拟网络请求的耗时操作
      var timer = setTimeout(function () {
        resolve(result);
        clearTimeout(timer)
      }, 500);
    });
  },

  refreshDatas: async function() {
    var pageSize = this.data.pageSize
    this.setData({
      loading:true
    })
    var result = await this.getList(1, pageSize)
    var list = result.list
    var totalCount = result.totalCount
    //计算总页数
    var totalPage = 1
    if (totalCount % pageSize > 0) {
      totalPage = Math.floor(totalCount / pageSize) + 1
    } else {
      totalPage = totalCount / pageSize
    }

    this.setData({
      loading: false,
      list: list,
      totalPage: totalPage,
      start: 1
    })
  },
  
  onRefresh: function(e) {
    this.refreshDatas()
  },

  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this
    var timer = setTimeout(function() {
      that.loadDatas()
      clearTimeout(timer)
    }, 500)
  },

  loadDatas: async function() {
    var pageSize = this.data.pageSize
    var that = this
    var start = that.data.start
    var totalPage = that.data.totalPage

    start += 1

    if (start > totalPage) {
      this.setData({
        show:true
      })
      return
    }

    wx.showLoading({
      title: '加载中',
    })
    var result = await this.getList(start, pageSize)
    var newList = result.list
    var totalCount = result.totalCount
    //计算总页数
    var totalPage = 1
    if (totalCount % pageSize > 0) {
      totalPage = Math.floor(totalCount / pageSize) + 1
    } else {
      totalPage = totalCount / pageSize
    }

    //追加数据
    var list = this.data.list
    for (var i = 0; i < newList.length; i++) {
      if(i < newList.length){
        list.push(newList[i])
        wx.showLoading({title:'数据读取中...'})
      }else{
        this.setData({
          show:true
        })
      }
      wx.hideLoading();
    }

    wx.hideLoading()

    this.setData({
      list: list,
      totalPage: totalPage,
      start: start
    })
  },
  //上拉加载 下拉刷新结束


  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})